package com.t2t.top.framework.util.page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangpengfei on 2016/3/4.
 * <p>
 * 通用分页处理类
 */
public class PageUtils {

    public final static Integer DEFAULT_SIZE = 100;

    /**
     * 分页执行数据
     *
     * @param list 全量数据
     * @param call 回调方法
     */
    public static void exec(List list, PageCallBack call) {
        exec(list, DEFAULT_SIZE, call);
    }

    public static void exec(List list, int pageSize, PageCallBack call) {
        Integer total = list.size();

        Page page = new Page(1, total, pageSize);
        int pages = page.getTotalPages();

        for (int i = 0; i < pages; i++) {
            page = new Page(i + 1, total, pageSize);
            System.out.println(page.getCurrentPage() + "," + page.getBegin() + "," + page.getEnd());
            List item = new ArrayList();
            for (int j = page.getBegin(); j <= page.getEnd(); j++) {
                item.add(list.get(j));
            }
            call.execute(item);

        }
    }

}

